import 'package:flutter/material.dart';
import 'package:flutter/rendering.dart';
import 'package:flutter_demo/common/CommonMethod.dart';
import 'package:flutter_demo/common/UserModel.dart';
import 'package:flutter_demo/models/user.dart';
import 'package:provider/provider.dart';

class MyDrawer extends StatelessWidget {

  MyDrawer({Key key}) : super(key:key);

  @override
  Widget build(BuildContext context) {
    return Drawer(
      child:
      MediaQuery.removePadding(
          context: context,
          removeTop: true,
          child: Column(
            crossAxisAlignment: CrossAxisAlignment.start,
            // 这里需要设置菜单中的内容只能占据一定的位置
            children: <Widget>[_getHeaderWidget(), Expanded(child: _buildMenus())],
          )),
    );
  }

  // 设置头像的widget
  Widget _getHeaderWidget() {
    return Consumer<UserModel>(
        builder: (BuildContext context, UserModel value, Widget child) {
      return GestureDetector(
        child: Container(
          color: Theme.of(context).primaryColor,
          padding: EdgeInsets.only(top: 40, bottom: 20),
          child: Row(
            children: <Widget>[
              Padding(
                padding: EdgeInsets.symmetric(horizontal: 16),
                child: ClipOval(
                  child: value.user == null ?
                  Image.asset("static/gsy_cat.png", width: 80,):
                  gmAvatar(value.user.avatar_url, width: 80)),
                ),
              Text(
                value.user == null ? "登陆" : value.user.name,
                style: TextStyle(
                  fontWeight: FontWeight.bold,
                  color: Colors.white
                ),
              )
            ],
          ),
        ),
        onTap: () {
          // 跳转到登录页面
          if (value.user == null) {
            Navigator.of(context).pushNamed("login");
          }
        },
      );
    });
  }

  Widget _buildMenus() {
    return Consumer<UserModel>(
      builder: (BuildContext context, UserModel userModel, Widget child) {
        return ListView(
          children: <Widget>[
            ListTile(
              leading: const Icon(Icons.color_lens),
              title: Text("主题"),
              onTap: () => Navigator.pushNamed(context, "themes"),
            ),
            if(userModel.user != null) ListTile(
              leading: const Icon(Icons.power_settings_new),
              title: Text(""),
              onTap: () {
                showDialog(
                  context: context,
                  builder: (ctx) {
                    //退出账号前先弹二次确认窗
                    return AlertDialog(
                      content: Text("确认退出吗"),
                      actions: <Widget>[
                        FlatButton(
                          child: Text("取消"),
                          onPressed: () => Navigator.pop(context),
                        ),
                        FlatButton(
                          child: Text("确定"),
                          onPressed: () {
                            //该赋值语句会触发MaterialApp rebuild
                            userModel.user = null;
                            Navigator.pop(context);
                          },
                        ),
                      ],
                    );
                  },
                );
              },
            ),
          ],
        );
      },
    );
  }
}
